﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Pub;
using System.Data;
using System.Text;

namespace JCNT.NoteWeb.Web
{
    public partial class Tree_Json : System.Web.UI.Page
    {
        public string sLab = "";
        public string sKey = "";
        public string sSQL = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            CDebug.EnterMethod("加载页面");
            try
            {
                CDebug.Send(Request.Url.ToString());
                if (Request["Lab"] != null) sLab = Request["Lab"];
                if (Request["Key"] != null) sKey = Request["Key"];
                if (Request["sql"] != null) sSQL = Request["sql"];

                //sKey = Microsoft.JScript.GlobalObject.escape(sKey);
                //sSQL = Microsoft.JScript.GlobalObject.escape(sSQL);
                sKey = HttpUtility.UrlDecode(sKey, Encoding.GetEncoding("gb2312"));
                sSQL = HttpUtility.UrlDecode(sSQL, Encoding.GetEncoding("gb2312"));

                if (!IsPostBack)
                {
                    getTit_Json();
                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

            CDebug.ExitMethod("加载页面");
        }

        public void getTit_Json()
        {
            CDebug.EnterMethod("加载数据");
            try
            {
                CADO ado1 = new CADO();
                string strSQL = "SELECT ID, Lab, Tit FROM wp_Note ";

                if (sKey != string.Empty)
                {

                    if (sLab == "TIT") strSQL += "WHERE Tit LIKE '%" + sKey + "%' ";
                    else if (sLab == "TXT") strSQL += "WHERE Con LIKE '%" + sKey + "%' ";
                    else if (sLab == "ALL") strSQL += "WHERE Tit LIKE '%" + sKey + "%' OR Con LIKE '%" + sKey + "%' ";
                }
                else if (sSQL != string.Empty)
                    strSQL = sSQL;
                else strSQL = "";

                DataSet ds1 = null;
                if (strSQL != string.Empty)
                {
                    ds1 = ado1.sel1(strSQL);
                    CDebug.Send(strSQL + "\n返回结果个数: " + ado1.ds_count);
                }


                int i_Count = 0;
                DataRow[] drs = null;
                if (!CDB.dsIsNull(ds1))
                {
                    drs = ds1.Tables[0].Select("1=1");
                    i_Count = drs.Length;
                }

                string sJson = "";
                sJson += "{\"total\":" + i_Count + ",\"rows\":[";

                for (int i = 0; i < i_Count; i++)
                {

                    DataRow dr = drs[i];

                    string sID = dr["ID"].ToString();
                    string sLab = dr["Lab"].ToString().Trim();
                    string sTit = dr["Tit"].ToString().Trim();

                    //sLab = "<xmp>" + sLab + "</xmp>";
                    //sTit = "<xmp>" + sTit + "</xmp>";

                    sLab = CDB.stringToJson(sLab);
                    sTit = CDB.stringToJson(sTit);

                    if (i != 0) sJson += ",";
                    sJson += "{\"ID\":\"" + sID + "\",\"Lab\":\"" + sLab + "\",\"Tit\":\"" + sTit + "\"}";
                    //CDebug.Send("[" + sTit + "]");
                }

                sJson += "]}";



                Response.Write(sJson);
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }
            CDebug.ExitMethod("加载数据");
        }
    }
}